<?php
//建立数据库连接
require '../script/mysqlconfig.php';
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
    die("连接数据库失败: " . $conn->connect_error);
}

//判断是否传递brand参数，存在则继续后面的插入操作
if (isset($_POST['cabinet']) ) {
    $value1 = trim($_POST["cabinet"]);
    $value2 = trim($_POST["cabinet2"]);
    //$brand不能为空值，品牌字段要排除空值
    if (empty($value1) || empty($value2) ) {
        echo "字段不能为空，添加失败！" . "<br/>";
    } else {
        $sql = "INSERT INTO cabinet (name, room) VALUES ('$value1', '$value2')";

        if ($conn->query($sql) === true) {
            echo "字段添加成功。";
        } else {
            echo "字段添加失败！" . "<br/>" . $conn->error;
        }
    }
} else {
    if (isset($_POST['room']) ) {
        $value1 = $_POST["room"];
        //POST没有传递brand参数，表示只是查询
        $sql = "SELECT * FROM cabinet where room = '$value1' ";
        $result = $conn->query($sql);
       // 生成表格
        if ($result->num_rows > 0) {
            echo "<table border='1' style='border-collapse: collapse;'>";
            echo "<tr><th>机柜编号</th><th>机房名称</th></tr>";
            while ($row = $result->fetch_assoc()) {
                echo "<tr><td>".$row["name"]."</td><td>".$row["room"]."</td></tr>";
            }
            echo "</table>";
        } else {
            echo "在数据库中没有查到数据。请检查输入的机房名称是否准确！";
        }
    }
}
//关闭数据库连接
$conn->close();
